<template>
  <uni-popup
    ref="popupSelectRef"
    mask-background-color="rgba(0,0,0,0.15)"
    :safe-area="false"
    type="bottom"
    @change="onVisibleChange"
  >
    <div class="popup-content">
      <span class="title">{{ title }}</span>
      <div v-if="filterable" class="search">
        <input
          v-model="keyword"
          class="uni-input iconfont icon-sousuoxiao"
          confirm-type="search"
          placeholder="搜索"
          @input="onSearch"
          @confirm="onSearch"
        />
      </div>
      <scroll-div
        v-if="optionsDisplaying.length > 0"
        scroll-y
        :scroll-top="scrollTop"
        class="list"
      >
        <radio-group>
          <div
            v-for="(item, idx) in optionsDisplaying"
            :key="item[idKey]"
            class="li"
            @click="onSelect(item, idx)"
          >
            <div>{{ item[labelKey] }}</div>
            <radio color="#899af4" :checked="selectedId === item[idKey]" />
          </div>
        </radio-group>
      </scroll-div>
      <div v-else style="text-align: center; margin-top: 20px; color: #666">
        暂无数据
      </div>
    </div>
  </uni-popup>
</template>

<script>
export default {
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    filterable: {
      type: Boolean,
      default: false,
    },
    title: {
      type: String,
      default: '请选择',
    },
    options: {
      type: Array,
      default: () => [],
    },
    idKey: {
      type: String,
      default: 'id',
    },
    labelKey: {
      type: String,
      default: 'label',
    },
  },
  data() {
    return {
      selectedId: '',
      keyword: '',
    }
  },
  computed: {
    optionsDisplaying() {
      return this.filterable
        ? this.options.filter(item =>
          item[this.labelKey].includes(this.keyword),
        )
        : this.options
    },
  },
  watch: {
    visible(val) {
      this.$refs.popupSelectRef[val ? 'open' : 'close']()
      this.keyword = ''
    },
    options() {
      this.selectedId = this.keyword = ''
    },
  },
  methods: {
    onSearch(e) {
      this.keyword = e.detail.value
    },
    onSelect(item, idx) {
      this.selectedId = item[this.idKey]
      this.$emit('change', item, idx)
    },
    onVisibleChange(e) {
      this.$emit('update:visible', e.show)
    },
  },
}
</script>

<style lang="scss" scoped>
.popup-content {
  box-sizing: border-box;
  height: 75vh;
  width: 100vw;
  background-color: #fff;
  border-radius: 20px 20px 0px 0px;
  padding: 17px 15px 11px 15px;

  .title {
    display: flex;
    justify-content: center;
    flex-wrap: wrap;
    font-size: 16px;
    color: #60687e;
    margin-bottom: 14px;
    font-weight: 600;
    width: 100%;
    height: 24px;
  }
  .search {
    margin-bottom: 8px;
    .uni-input {
      display: flex;
      align-items: center;
      background-color: #f5f6f8;
      width: 100%;
      height: 35px;
      padding: 0px 9px;
      border-radius: 4px;
    }
  }
  .list {
    width: 100%;
    overflow-y: auto;
    height: calc(75vh - 100px);
    radio-group {
      width: 100%;
    }
    .li {
      padding: 13px 10px 13px 13px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      box-shadow: 0px 0px 6px 0px rgba(0, 0, 0, 0.1);
      margin-bottom: 10px;
      radio {
        transform: scale(0.7);
      }
    }
  }
}
</style>
